﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Qiang.QMath
{
    /// <summary>
    /// 邻居们。存储离目标点最近的点。使用队列存储
    /// </summary>    
    public class KdNeighbors
    {

        public KdNeighbors(int capacity)
        {
            this._capacity = capacity;
            this._queue = new KdQueue(capacity, double.PositiveInfinity);
        }
        

        private int _capacity;

        private KdQueue _queue;


        /// <summary>
        /// 插入一个新元素
        /// </summary>
        /// <param name="element"></param>
        /// <param name="prioity"></param>
        /// <returns></returns>
        public bool Insert(object element, double prioity)
        {
            //如果队列还没满，则添加新元素
            if (this._queue.Length() < this._capacity)
            {
                this._queue.Add(element, prioity);
                return true; //添加成功
            }

            //如果队列满了，则判断新添加元素与队列最高优先级元素的优先级别
            if (prioity > this._queue.GetMaxPriority())
            {
                return false; //没有添加，失败
            }

            //如果队列最高优先级元素的优先级大于新元素的优先级则删除最大，添加校报元素
            this._queue.Remove();
            this._queue.Add(element, prioity);
            return true;

        }


        /// <summary>
        /// 标志队列是否饱和了
        /// </summary>
        public bool IsCapacityReached
        {
            get 
            {
                return this._queue.Length() >= this._capacity;
            }
        }




    }

}
